//获取应用实例
var app = getApp();
var index = 1;
Page({
  data: {
    list: [],
    hasMore: true,
    // 是否显示loading
    showLoading: false,
    // loading提示语
    loadingMessage: '',
    // 是否显示toast
    showToast: false,
    // 提示消息
    toastMessage: '',

    canSee: false,

    touch_end: 0,
    touch_start: 0,
  },
 
  //按下事件开始
  mytouchstart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束
  mytouchend: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },
  // 显示loading提示
  showLoading(loadingMessage) {
    // this.setData({ showLoading: true, loadingMessage });
  },

  // 隐藏loading提示
  hideLoading() {
    this.setData({ showLoading: false, loadingMessage: '' });
  },

  // 显示toast消息
  showToast(toastMessage) {
    this.setData({ showToast: true, toastMessage });
  },

  // 隐藏toast消息
  hideToast() {
    this.setData({ showToast: false, toastMessage: '' });
  },
  onLoad:function(){
    // wx.clearStorageSync();
    this.onGetOpenid()
    this.onQuery()
  },
  onQuery: function(){
      let that = this;
  
      const db = wx.cloud.database()
      db.collection('profiles')
      .get()
      .then(res => {
        console.log('[数据库] [查询记录] 成功: ', res)
        let items = res.data;
        this.setData({
          list: items
        })
      })
      .catch(err=>{
        console.error('[数据库] [查询记录] 失败：', err)
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        
      })
  },
  onGetOpenid: function() {
    let that = this;
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {}
    })
    .then(res=>{
      console.log('[云函数] [getTime]: ', res)
      app.globalData.openid = res.result.openid;
    })
    .catch(err=>{
      console.error('[云函数] [getTime] 调用失败', err)
      wx.navigateTo({
        url: '../deployFunctions/deployFunctions',
      })
    })
  },
  onShow: function () {
    this.onQuery()
    // wx.clearStorageSync();
    let that = this
    wx.stopPullDownRefresh();
    wx.hideNavigationBarLoading();

    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo);
    if (!userInfo) {
      wx.navigateTo({
        url: "../auth/auth"
      })
    } else {
      // wx.showNavigationBarLoading()
      // app.getCity(function () {
      //   wx.hideNavigationBarLoading()
      //   wx.setNavigationBarTitle({
      //     title: '通讯录 - ' + app.globalData.city
      //   })
      // })
      var self = this;
   
    }
  },
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    wx.showNavigationBarLoading();
  },
  onReachBottom: function () {
    var that = this
    if (!that.data.showLoading) {
      wx.showNavigationBarLoading();

    }
  },
  toAddtxl: function (event) {
    wx.navigateTo({
      url: "../addCategory/addCategory"
    })
  }
})

var deleteclassifys = function (classifyGuid, self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'classify/deleteClassify', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        classifyGuids: classifyGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        });
        wx.showNavigationBarLoading()
        index = 1;
        getData(self);
      } else {
        wx.showToast({
          title: '删除失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}

var getUserByUserGuid = function (self, userguid) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/getUserByUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userGuid: userguid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.isAdmin == 1) {
          self.setData({
            canSee: true
          })
        }
      }
    }
  })
}